import Vue from 'vue'
import {Message} from 'element-ui'
/**
 * v-copy 拷贝
 */
function copyToClipboard(text) {
  let textarea = document.createElement('textarea');
  textarea.style.position = 'fixed';
  textarea.style.opacity = 0;
  textarea.value = text;
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand('copy');
  document.body.removeChild(textarea);
  Message.success('复制成功')
}

export default {
  inserted(el, binding, vnode) {
    const {value} = binding
    let text = value ? value : el.innerText
    if(text !== "" || el.childNodes.length !== 0 && el.childNodes[0].length > 0){
      let span = document.createElement('i')
      span.className = "el-icon-copy-document"
      el.appendChild(span)
      let clickCopy = (e) => {
        e.stopPropagation()
        copyToClipboard(text)
      }
      span.onclick = clickCopy
      el.onclick = clickCopy
    }
  }
}

